const path = require("path")
const webpack = require('webpack')
const {
    CleanWebpackPlugin
} = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin");

class MyPlugin {
    apply(compiler) {
        console.log("MyPlugin 启动");
        compiler.hooks.emit.tap("MyPlugin", compilation => {
            // compilation => 可以理解为此次打包的上下文
            for (const name in compilation.assets) {
                // console.log(name)
                // console.log(compilation.assets[name].source());
                if (name.endsWith(".js")) {
                    console.log(name);
                    const contents = compilation.assets[name].source()
                    const withOutComments = contents.replace(/\/\*\*+\*\//g, '')
                    compilation.assets[name] = {
                        source: () => withOutComments,
                        size: () => withOutComments.length
                    }
                }
            }
        })
    }
}

module.exports = (env, argv) => {
    const config = {
        mode: "none",
        entry: "./src/main.js",
        output: {
            filename: "bundle.js",
            path: path.join(__dirname, "dist"),
            // publicPath: "dist/"
        },
        devtool: "source-map", 
        devServer: {
            hot: true,
            contentBase: "./public",
            proxy: {
                "/api": {
                    // http://localhost:8080/api/users => https://api.github.com/api/users
                    target: "https://api.github.com",
                    // http://localhost:8080/api/users => https://api.github.com/users
                    pathRewrite: {
                        '^/api': ''
                    },
                    // 不能使用 localhost:8080 作为请求 Github 的主机名
                    changeOrigin: true
                }
            }
        },
        module: {
            rules: [{
                test: /.md$/,
                use: [
                    "html-loader",
                    "./markdown-loader"
                ]
            }]
        },
        plugins: [
            // new CleanWebpackPlugin(),
            // 用于生成index.html
            new HtmlWebpackPlugin({
                title: "Webpack Plugin Sample",
                meta: {
                    viewport: "width=device-width"
                },
                template: "./src/index.html"
            }),
            // 用于生成 about.html
            new HtmlWebpackPlugin({
                filename: "about.html"
            }),
            // new CopyWebpackPlugin({
            //     patterns: [
            //         {
            //             from: "public"
            //         }
            //     ]
            // }),
            new MyPlugin(),
            new webpack.HotModuleReplacementPlugin()
        ]
    }

    if (env === "production") {
        config.mode = 'production'
        config.devtool = false
        config.plugins = [
            ...config.plugins,
            new CleanWebpackPlugin(),
            new CopyWebpackPlugin({
                patterns: [
                    {
                        from: "public"
                    }
                ]
            })
        ]
    }
    return config
}